package com.hz.monitoring.module.hardware.controller.admin.warning_contact;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.framework.common.pojo.CommonResult;
import static com.hz.monitoring.framework.common.pojo.CommonResult.success;

import com.hz.monitoring.framework.excel.core.util.ExcelUtils;

import com.hz.monitoring.framework.operatelog.core.annotations.OperateLog;
import static com.hz.monitoring.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.hz.monitoring.module.hardware.controller.admin.warning_contact.vo.*;
import com.hz.monitoring.module.hardware.dal.dataobject.warning_contact.WarningContactDO;
import com.hz.monitoring.module.hardware.convert.warning_contact.WarningContactConvert;
import com.hz.monitoring.module.hardware.service.warning_contact.WarningContactService;

@Api(tags = "管理后台 - 预警通讯方式管理")
@RestController
@RequestMapping("/hardware/warning-contact")
@Validated
public class WarningContactController {

    @Resource
    private WarningContactService warningContactService;

    @PostMapping("/create")
    @ApiOperation("创建预警通讯方式管理")
    @PreAuthorize("@ss.hasPermission('hardware:warning-contact:create')")
    public CommonResult<Long> createWarningContact(@Valid @RequestBody WarningContactCreateReqVO createReqVO) {
        return success(warningContactService.createWarningContact(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新预警通讯方式管理")
    @PreAuthorize("@ss.hasPermission('hardware:warning-contact:update')")
    public CommonResult<Boolean> updateWarningContact(@Valid @RequestBody WarningContactUpdateReqVO updateReqVO) {
        warningContactService.updateWarningContact(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除预警通讯方式管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:warning-contact:delete')")
    public CommonResult<Boolean> deleteWarningContact(@RequestParam("id") Long id) {
        warningContactService.deleteWarningContact(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得预警通讯方式管理")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:warning-contact:query')")
    public CommonResult<WarningContactRespVO> getWarningContact(@RequestParam("id") Long id) {
        WarningContactDO warningContact = warningContactService.getWarningContact(id);
        return success(WarningContactConvert.INSTANCE.convert(warningContact));
    }

    @GetMapping("/list")
    @ApiOperation("获得预警通讯方式管理列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('hardware:warning-contact:query')")
    public CommonResult<List<WarningContactRespVO>> getWarningContactList(@RequestParam("ids") Collection<Long> ids) {
        List<WarningContactDO> list = warningContactService.getWarningContactList(ids);
        return success(WarningContactConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得预警通讯方式管理分页")
    @PreAuthorize("@ss.hasPermission('hardware:warning-contact:query')")
    public CommonResult<PageResult<WarningContactRespVO>> getWarningContactPage(@Valid WarningContactPageReqVO pageVO) {
        PageResult<WarningContactDO> pageResult = warningContactService.getWarningContactPage(pageVO);
        return success(WarningContactConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出预警通讯方式管理 Excel")
    @PreAuthorize("@ss.hasPermission('hardware:warning-contact:export')")
    @OperateLog(type = EXPORT)
    public void exportWarningContactExcel(@Valid WarningContactExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WarningContactDO> list = warningContactService.getWarningContactList(exportReqVO);
        // 导出 Excel
        List<WarningContactExcelVO> datas = WarningContactConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "预警通讯方式管理.xls", "数据", WarningContactExcelVO.class, datas);
    }

}
